package com.cos.blog.dao.impl;

import com.cos.blog.core.utils.DataSourceUtils;
import com.cos.blog.dao.IUserDao;
import com.cos.blog.entity.UserEntity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.util.List;

public class UserDaoImpl implements IUserDao {

    private QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

    @Override
    public Integer insert(UserEntity userEntity) throws Exception {
        String sql = "insert into t_user(username,password,email,type,status,activation_code,create_time) values(?,?,?,?,?,?,?)";
        return qr.update(sql,userEntity.getUsername(),userEntity.getPassword(), userEntity.getEmail(),
                userEntity.getType(), userEntity.getStatus(),userEntity.getActivationCode(),
                userEntity.getCreateTime());
    }

    @Override
    public UserEntity selectById(Integer id) throws Exception {
        String sql = "select u.*,u.header_url as headerUrl from t_user u where id = ?";
        return qr.query(sql,new BeanHandler<UserEntity>(UserEntity.class),id);
    }

    @Override
    public Integer updateById(UserEntity userEntity) throws Exception {
        String sql = "update t_user set username = ?,email = ?,status = ? where id = ?";
        return qr.update(sql, userEntity.getUsername(),
                userEntity.getEmail(), userEntity.getStatus(), userEntity.getId());
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<UserEntity> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchRemove(List<Integer> ids) throws Exception {
        return null;
    }

    /**
     * @Author:zxc
     * @Description: 激活
     */
    @Override
    public UserEntity selectByActivationCode(String activationCode) throws Exception {
        String sql = "select * from t_user where activation_code = ?";
        return qr.query(sql, new BeanHandler<UserEntity>(UserEntity.class), activationCode);
    }

    /**
     * @Author:zxc
     * @Description: 登录
     */
    @Override
    public UserEntity selectByUsername(String username) throws Exception {
        String sql = "select * from t_user where username = ?";
        return qr.query(sql, new BeanHandler<UserEntity>(UserEntity.class), username);
    }

    /**
     * @Author:zxc
     * @Description: 更新标题url
     */
    @Override
    public void updateHeaderUrl(Integer id, String filePath) throws Exception {
        String sql = "update t_user set header_url = ? where id = ?";
        qr.update(sql, filePath, id);
    }
}
